//
//  Element.cpp
//  PlantNannyDemo
//
//  Created by Joueu on 14-10-24.
//
//

#include "Element.h"


Element *Element::createWithItemNameAndNum(int num,const char* itemName)
{
    Element *element = new Element();
    if  (element && element -> initWithItemNameAndNum(num,itemName))
    {
        element -> autorelease();
        return element;
    }
    else {
        CC_SAFE_RELEASE(element);
        return nullptr;
    }
}

bool Element::initWithItemNameAndNum(int num,const char* itemName)
{
    if (!ui::Button::init("elementbg.png")) {
        return false;
    }
    
    m_leftItem = Sprite::create(itemName);
    m_leftItem -> setAnchorPoint(Point(0.65, 0.32));
    m_leftItem -> setPosition(Point(0,this -> getContentSize().height * 0.5));
    this -> addChild(m_leftItem, 5);
    
    this -> setCascadeOpacityEnabled(true);
    
    m_numLabel = Label::createWithSystemFont(StringUtils::format("%d",num), "微软雅黑", 20);
    m_numLabel -> setAnchorPoint(Point(0.5,0.5));
    m_numLabel -> setTextColor(Color4B(0, 0, 0, 255));
    m_numLabel -> setPosition(Point(this -> getContentSize().width *0.5, this -> getContentSize().height * 0.5f));
    this -> addChild(m_numLabel,10);
    
    return true;
}

void Element::updateDate(int addend)
{
    m_num = m_num + addend;
    
    m_numLabel -> setString(StringUtils::format("%d",m_num));
}
